using MediatR;
using SumerCoreDevOps.Application.SystemConfigs.Commands;
using SumerCoreDevOps.Application.SystemConfigs.Queries;
using SumerCoreDevOps.Domain.Entities;
using SumerCoreDevOps.Infrastructure.Configuration;

namespace SumerCoreDevOps.Application.SystemConfigs.Handlers;

public class UpdateConfigCommandHandler : IRequestHandler<UpdateConfigCommand, bool>
{
    private readonly SystemConfigManager _configManager;

    public UpdateConfigCommandHandler(SystemConfigManager configManager)
    {
        _configManager = configManager;
    }

    public async Task<bool> Handle(UpdateConfigCommand request, CancellationToken cancellationToken)
    {
        return await _configManager.UpdateConfigAsync(
            request.ConfigKey,
            request.ConfigValue,
            request.UpdatedBy
        );
    }
}

public class UpdateConfigsCommandHandler : IRequestHandler<UpdateConfigsCommand, Dictionary<string, bool>>
{
    private readonly SystemConfigManager _configManager;

    public UpdateConfigsCommandHandler(SystemConfigManager configManager)
    {
        _configManager = configManager;
    }

    public async Task<Dictionary<string, bool>> Handle(UpdateConfigsCommand request, CancellationToken cancellationToken)
    {
        return await _configManager.UpdateConfigsAsync(request.Configs, request.UpdatedBy);
    }
}

public class CreateConfigCommandHandler : IRequestHandler<CreateConfigCommand, SystemConfig>
{
    private readonly SystemConfigManager _configManager;

    public CreateConfigCommandHandler(SystemConfigManager configManager)
    {
        _configManager = configManager;
    }

    public async Task<SystemConfig> Handle(CreateConfigCommand request, CancellationToken cancellationToken)
    {
        return await _configManager.CreateConfigAsync(
            request.ConfigKey,
            request.ConfigValue,
            request.ConfigType,
            request.Category,
            request.Description,
            request.RequiresRestart,
            request.IsSensitive,
            request.CreatedBy
        );
    }
}

public class DeleteConfigCommandHandler : IRequestHandler<DeleteConfigCommand>
{
    private readonly SystemConfigManager _configManager;

    public DeleteConfigCommandHandler(SystemConfigManager configManager)
    {
        _configManager = configManager;
    }

    public async Task Handle(DeleteConfigCommand request, CancellationToken cancellationToken)
    {
        await _configManager.DeleteConfigAsync(request.ConfigKey);
    }
}

public class GetConfigQueryHandler : IRequestHandler<GetConfigQuery, SystemConfig?>
{
    private readonly SystemConfigManager _configManager;

    public GetConfigQueryHandler(SystemConfigManager configManager)
    {
        _configManager = configManager;
    }

    public async Task<SystemConfig?> Handle(GetConfigQuery request, CancellationToken cancellationToken)
    {
        return await _configManager.GetConfigAsync(request.ConfigKey);
    }
}

public class GetConfigValueQueryHandler : IRequestHandler<GetConfigValueQuery, string?>
{
    private readonly SystemConfigManager _configManager;

    public GetConfigValueQueryHandler(SystemConfigManager configManager)
    {
        _configManager = configManager;
    }

    public async Task<string?> Handle(GetConfigValueQuery request, CancellationToken cancellationToken)
    {
        if (request.DefaultValue != null)
        {
            return await _configManager.GetConfigValueAsync(request.ConfigKey, request.DefaultValue);
        }

        return await _configManager.GetConfigValueAsync(request.ConfigKey);
    }
}

public class GetAllConfigsQueryHandler : IRequestHandler<GetAllConfigsQuery, List<SystemConfig>>
{
    private readonly SystemConfigManager _configManager;

    public GetAllConfigsQueryHandler(SystemConfigManager configManager)
    {
        _configManager = configManager;
    }

    public async Task<List<SystemConfig>> Handle(GetAllConfigsQuery request, CancellationToken cancellationToken)
    {
        return await _configManager.GetAllConfigsAsync();
    }
}

public class GetConfigsByCategoryQueryHandler : IRequestHandler<GetConfigsByCategoryQuery, List<SystemConfig>>
{
    private readonly SystemConfigManager _configManager;

    public GetConfigsByCategoryQueryHandler(SystemConfigManager configManager)
    {
        _configManager = configManager;
    }

    public async Task<List<SystemConfig>> Handle(GetConfigsByCategoryQuery request, CancellationToken cancellationToken)
    {
        return await _configManager.GetConfigsByCategoryAsync(request.Category);
    }
}
